package com.king.netty.heartbeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * 自定义 Handler
 * @author unidentifiable
 * @date 2021-03-31
 */
public class MyIdleStateServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateHandler){
            IdleStateEvent event = (IdleStateEvent) evt;
            String msg = "";
            switch (event.state()){
                case READER_IDLE:
                    msg = "长时间没进行读操作";
                    break;
                case WRITER_IDLE:
                    msg = "长时间没进行写操作";
                    break;
                case ALL_IDLE:
                    msg = "长时间没进行读写操作";
                    break;
                default:
                    break;
            }

            System.out.println(ctx.channel().remoteAddress() + "：" + msg);
        }
    }
}
